package com.hugoo.form.buzz;

import com.hugoo.core.Conf;
import com.hugoo.core.db.HSF;
import com.hugoo.core.db.mo.Record;
import com.hugoo.core.db.mo.RecordSet;
import com.hugoo.core.util.TL;
import java.util.Map;
import java.util.Stack;

/**
 *
 * @author Lovelock.Chan
 */
public class XTreeBuzz {

    private final String sid, dbl;
    private String tpl;

    public XTreeBuzz(String sid, Map m) {
        this.sid = sid;
        _init(m);
        dbl = Conf.getBaseProp(sid, Conf.TREE._xx_dbl);
    }

    private void _init(Map m) {
        StringBuilder sb = new StringBuilder();
        String id = Conf.getBaseProp(sid, Conf.TREE._xx_id);
        String desc = Conf.getBaseProp(sid, Conf.TREE._xx_desc);
        String para = Conf.getBaseProp(sid, Conf.TREE._xx_para);
        String tbl = Conf.getBaseProp(sid, Conf.TREE._xx_table);
        if (TL.isNotEmpty(para)) {
            String[] parax = para.toUpperCase().split(",");
            for (String x : parax) {
                tbl = tbl.replaceAll(":" + x, flt(m.get(x)));
            }
        }
        String sw = Conf.getBaseProp(sid, Conf.TREE._xx_sw);
        String prp = Conf.getBaseProp(sid, Conf.TREE._xx_prior);
        String key = Conf.getBaseProp(sid, Conf.TREE._xx_key);
        sb.append("SELECT 'E'||CONNECT_BY_ISLEAF  XX__CE,'F'||LEVEL XX__CF,").append(id).append(" XX__CD,").
                append(desc).append(" XX__CS,'H'||INSTR('").
                append(flt(m.get(key))).append("',").append(id).append("||',') XX__CH FROM ").append(tbl).
                append(" START WITH ").append(sw).append(" CONNECT BY PRIOR ").append(prp);
        tpl = sb.toString();
    }

    public String toJson() {
        RecordSet<Record> rst = HSF.query(dbl, tpl);
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        String e, f, d, s, h, p = null;
        boolean m = true;
        Stack<String> sk = new Stack<String>();
        for (Record r : rst) {
            e = r.getString("XX__CE");
            f = r.getString("XX__CF");
            d = r.getString("XX__CD");
            s = r.getString("XX__CS");
            h = r.getString("XX__CH");
            if (p != null) {//去掉第一条记录
                if (p.equals(f)) {
                    //如果P不为空且和当前值为同一等级
                    sb.append(",");
                } else {// 如果前置值和当前值为同一等级
                    if (!sk.empty()) {
                        if (sk.peek().compareTo(f) < 0) {
                            //下级
                        } else {
                            //上级
                            do {
                                if (m) {
                                    sb.append("],\"state\":\"closed\"}");
                                } else {
                                    sb.append("]}");
                                }
                            } while (!f.equals(sk.pop()));
                            m = true;
                            sb.append(",");
                        }
                    }

                }
            }
            sb.append("{\"id\":\"").append(d).append("\"");
            sb.append(",\"text\":\"").append(s).append("\"");
            if (!"H0".equals(h)) {
                m = false;
                sb.append(",\"checked\":true");
            }
            if ("E0".equals(e)) {//存在子节点
                sk.push(f);
                sb.append(",\"children\":[");
            } else {//
                sb.append("}");
            }
            p = f;
        }
        if (!sk.empty()) {
            do {//收尾工作
                p = sk.pop();
                if (m && !"F1".equals(p)) {
                    sb.append("],\"state\":\"closed\"}");
                } else {
                    sb.append("]}");
                }
            } while (!sk.empty());
        }
        sb.append("]");
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println("F3".compareTo("F2"));
    }

    public String flt(Object obj) {
        if (obj == null) {
            return "";
        } else if (obj instanceof String) {
            return (String) obj;
        }
        return obj.toString();
    }
}
